var i = 10;

// Display desktop notifications
function Notification(synchronizer) {
	var notification;
	
	var breakObject;
	var isSoundEnabled = false;
	var notificationAudioFile;
	
	var boundCloseEventListener;
	
	this.init = function(currentBreakObject) {
		breakObject = currentBreakObject;
		
		isSoundEnabled  = breakObject.isSoundEnabled();
		notificationAudioFile = breakObject.getNotificationAudioFile();
		
		this.createNotification();
	};
	
	this.createNotification = function() {
		var nextBreakTime = new Date(new Date().getTime() + breakObject.getDuration()*1000 + synchronizer.getMicroBreak().getFrequency()*1000); 
		var breakDuration = breakObject.getDuration();
		
		notification = window.webkitNotifications.createHTMLNotification("Pages_oo/notificationWindow.html?nextBreakTime="
				+ nextBreakTime + "&breakDuration=" + breakDuration + "&soundEnabled=" + isSoundEnabled
				+ "&notificationAudioFile=" + notificationAudioFile);
		
		boundCloseEventListener = this.close.bind(this);
		notification.addEventListener('close', boundCloseEventListener, false);
		
		this.playNotificationSound(notificationAudioFile);
	};
	
	this.playNotificationSound = function(notificationAudioFile) {
		if(isSoundEnabled) {
			playSound(notificationAudioFile);
		}
	};
	
	this.requestPermission = function() {
		if (window.webkitNotifications.checkPermission() != 0) {
			window.webkitNotifications.requestPermission();
		}
	};

	this.isNotificationEnabled = function() {
		return (webkitNotifications.checkPermission() == 0) ? true : false;
	};

	this.display = function(currentBreakObject) {
		this.init(currentBreakObject);
		if (window.webkitNotifications.checkPermission() == 0) {
			// Check setting and play the notification sound
			notification.show();
		}
	};

	this.close = function() { 
		// Check if any settings were changed in the notification window and load them if any
		if(isSettingsStale()) {
			synchronizer.reloadSettings();
			this.init(breakObject);
		}
		markSettingsStale("false");
		
		this.playNotificationSound(notificationAudioFile);
		
		if (notification != null) {
			notification.removeEventListener('close', boundCloseEventListener, false); 
			notification.cancel();
			this.restartBreak();
		}
	};
	
	this.clear = function() {
		if (notification != null) {
			notification.removeEventListener('close', boundCloseEventListener, false);
			notification.cancel();
		}
	};
	
	this.restartBreak = function() {
		synchronizer.restartBreak();
	};
}